#include "dialog_create.h"
#include "ui_dialog_create.h"

#include <QMetaEnum>
#include <QDebug>

DialogCreate::DialogCreate(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogCreate)
{
    ui->setupUi(this);
    init();
}

DialogCreate::~DialogCreate()
{
    delete ui;
}

void DialogCreate::init()
{
    QSerialPort serialPort;
    QSerialPortInfo serialPortInfo;
    foreach(QSerialPortInfo portInfo, QSerialPortInfo::availablePorts()) {
        ui->comboBox_name->addItem(portInfo.portName());
    }
    QMetaEnum tenum = QMetaEnum::fromType<QSerialPort::BaudRate>();
    for(int i = 0; i < tenum.keyCount(); i++) {
        ui->comboBox_baud->addItem(tenum.key(i), tenum.value(i));
    }
    tenum = QMetaEnum::fromType<QSerialPort::Parity>();
    for(int i = 0; i < tenum.keyCount(); i++) {
        ui->comboBox_parity->addItem(tenum.key(i), tenum.value(i));
    }
    tenum = QMetaEnum::fromType<QSerialPort::DataBits>();
    for(int i = 0; i < tenum.keyCount(); i++) {
        ui->comboBox_databits->addItem(tenum.key(i), tenum.value(i));
    }
    ui->comboBox_databits->setCurrentIndex(ui->comboBox_databits->count()-2);
    tenum = QMetaEnum::fromType<QSerialPort::StopBits>();
    for(int i = 0; i < tenum.keyCount(); i++) {
        ui->comboBox_stopbits->addItem(tenum.key(i), tenum.value(i));
    }
}
QString DialogCreate::portName(){
    return ui->comboBox_name->currentText();
}
QSerialPort::BaudRate DialogCreate::baudRate(){
    return (QSerialPort::BaudRate)ui->comboBox_baud->currentData().toInt();
}
QSerialPort::Parity DialogCreate::parity(){
    return (QSerialPort::Parity)ui->comboBox_parity->currentData().toInt();
}
QSerialPort::DataBits DialogCreate::dataBits(){
    return (QSerialPort::DataBits)ui->comboBox_databits->currentData().toInt();
}
QSerialPort::StopBits DialogCreate::stopBits(){
    return (QSerialPort::StopBits)ui->comboBox_stopbits->currentData().toInt();
}


